﻿using System;
using System.Collections.Generic;
using System.Data;
using SAS.Core.Result;

namespace SAS.DataAccess
{
    public class EnvironmentDAO : BaseDAO
    {
        #region Contructor

        protected EnvironmentDAO()
        {
        }

        public EnvironmentDAO(string tokenKey)
            : base(tokenKey)
        {
        }

        #endregion

        public SASResult<IList<Core.Entity.Environment>> GetEnvironments(int pageNumber, int pageSize, string orderBy, string conditionList)
        {
            //Indentify view name
            const string viewName = "[scmAdminApp].[vwEnvironmentSummary]";

            //Indentify structure that parse SQL results to objects.
            Func<IDataReader, Core.Entity.Environment> make = reader => new Core.Entity.Environment
                                                                        {
                                                                            Id = Convert.ToInt32(reader["pkEnvironment_ID"]),
                                                                            Name = Convert.ToString(reader["EnvironmentName"]),
                                                                            Description = Convert.ToString(reader["EnvironmentDesc"]),
                                                                            DatabaseName = Convert.ToString(reader["DBName"]),
                                                                            ServiceUrl = Convert.ToString(reader["AdminSvcURL"]),
                                                                            CreatedBy = Convert.ToString(reader["CreatedBy"]),
                                                                            CreatedOn = Convert.ToDateTime(reader["CreatedDate"]).ToShortDateString(),
                                                                            LastModifiedBy = Convert.ToString(reader["ModifiedBy"]),
                                                                            EnvironmentURL = Convert.ToString(reader["WebAppURL"]),
                                                                            LastModifiedOn = reader["ModifiedDate"] == DBNull.Value
                                                                                   ? String.Empty : Convert.ToDateTime(reader["ModifiedDate"]).ToShortDateString()
                                                                        };

            return Search(pageNumber, pageSize, orderBy, conditionList, viewName, make);
        }
 
        public SASResult<IList<string>> GetEnvironmentList()
        {
            //Indentify SQL store procedure name
            const string columnName = "EnvironmentName";

            Func<IDataReader, string> make = reader => Convert.ToString(reader["EnvironmentName"]);
            return GetDropDownList(columnName, make);
        } 
    }
}